package com.xdf.zipperdye.service;

import com.xdf.zipperdye.dao.business.SysRoleMapper;
import com.xdf.zipperdye.model.SysRole;
import com.xdf.zipperdye.model.SysUserRole;
import com.xdf.zipperdye.utils.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper dao;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        SysRole role = dao.selectByPrimaryKey(iid);
        if (role == null) throw new BusinessException("未找到要删除的数据");

        List<SysUserRole> list = sysUserRoleService.selectByRoleIid(iid);
        if (list.size() > 0) throw new BusinessException("该角色有关联的用户存在，不可删除！");

        return dao.deleteByPrimaryKey(iid);
    }

    @Override
    public SysRole insert(SysRole record) {
        int ret = dao.insert(record);
        return record;
    }

    @Override
    public SysRole insertSelective(SysRole record) {
        int ret = dao.insertSelective(record);
        return record;
    }

    @Override
    public SysRole selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public SysRole updateByPrimaryKeySelective(SysRole record) {
        int ret = dao.updateByPrimaryKeySelective(record);
        return record;
    }

    @Override
    public SysRole updateByPrimaryKey(SysRole record) {
        int ret = dao.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public List<SysRole> selectAll() {
        return dao.selectAll();
    }

    @Override
    public SysRole selectBySid(String sid) {
        return dao.selectBySid(sid);
    }

    @Override
    public boolean isExist(String sid) {
        if (sid == null || sid.length() <= 0) return false;
        SysRole record = dao.selectBySid(sid);
        return record == null ? false : true;
    }

    @Override
    public List<SysRole> selectByPks(String pks) {
        return dao.selectByPks(pks);
    }

}